﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class DataSetDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var id = Request["id"];
                var dataset = Session["skole"] as System.Data.DataSet;
                System.Data.DataRow skola = null;
                foreach (System.Data.DataRow row in dataset.Tables[0].Rows)
                {
                    if (Convert.ToString(row["IDSkola"]) == id)
                        skola = row;
                }
                txtNaziv.Text = skola["Naziv"].ToString();
                txtAdresa.Text = skola["Adresa"].ToString();
                ddlZemlja.SelectedValue = skola["Zemlja"].ToString().Trim();
                ddlZemlja.DataBind();

                NapuniZemlje(skola["Zemlja"].ToString());
                
            }

        }

        private void NapuniZemlje(string selektovanaZemlja)
        {
            var connection = new System.Data.SqlClient.SqlConnection();
            connection.ConnectionString = @"Data Source=.\Katmai;Initial Catalog=Test;Integrated Security=True";
            var sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = "SELECT DISTINCT Zemlja FROM Skola";

            var dataset = new System.Data.DataSet();
            var adapter = new System.Data.SqlClient.SqlDataAdapter();
            adapter.SelectCommand = sqlCommand;
            adapter.Fill(dataset);
            ddlDinamickaLista.DataSource = dataset;
            ddlDinamickaLista.SelectedValue = selektovanaZemlja;
            ddlDinamickaLista.DataBind();
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            SnimiUSesiju();

            SnimiUBazu();

            Response.Redirect("DataSet.aspx");

        }

        /// <summary>
        /// Pronadje DataSet iz sesije, u njemu nadje skolu po id-ju i popuni podatke
        /// </summary>
        private void SnimiUSesiju()
        {
            var id = Request["id"];
            var dataset = Session["skole"] as System.Data.DataSet;
            System.Data.DataRow skola = null;
            foreach (System.Data.DataRow row in dataset.Tables[0].Rows)
            {
                if (Convert.ToString(row["IDSkola"]) == id)
                    skola = row;
            }
            skola["Naziv"] = txtNaziv.Text;
            skola["Adresa"] = txtAdresa.Text;
            skola["Zemlja"] = ddlZemlja.SelectedValue;
        }

        /// <summary>
        /// Napravi UPDATE upit i snimi promene u bazu
        /// </summary>
        private void SnimiUBazu()
        {
            var id = Request["id"];

            var connection = new System.Data.SqlClient.SqlConnection();
            connection.ConnectionString = @"Data Source=.\Katmai;Initial Catalog=Test;Integrated Security=True";
            var sqlCommand = connection.CreateCommand();

            var upit = string.Format( @"UPDATE Skola
                                        SET Naziv = '{0}',
                                            Adresa = '{1}',
                                            Zemlja = '{2}'
                                        WHERE IDSkola = {3}",
                                        txtNaziv.Text,              /* {0} */
                                        txtAdresa.Text,             /* {1} */
                                        ddlZemlja.SelectedValue,    /* {2} */
                                        id                          /* {3} */);
            sqlCommand.CommandText = upit;

            connection.Open();
            sqlCommand.ExecuteNonQuery();

            connection.Close();
        }
    }
}